#include <Windows.h>
#include <tchar.h>
#include <stdio.h>

#define IDC_BTNSETDATA	1000
#define IDC_BTNGETDATA	1001

HRESULT CALLBACK WndProc(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	switch (nMessage)
	{
		case WM_CREATE:
		{
			CreateWindow(_T("Button"), _T("SetData"), WS_CHILD|WS_VISIBLE, 0, 0, 150, 50, hWnd, (HMENU)IDC_BTNSETDATA, NULL, NULL);
			CreateWindow(_T("Button"), _T("GetData"), WS_CHILD|WS_VISIBLE, 160, 0, 150, 50, hWnd, (HMENU)IDC_BTNGETDATA, NULL, NULL);

			break;
		}
		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hDC = BeginPaint(hWnd, &ps);

			EndPaint(hWnd, &ps);
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNSETDATA:
				{
					if (OpenClipboard(hWnd))
					{
						TCHAR* szTmp	= _T("open clip board");
						UINT nSize		= (_tcslen(szTmp) + 1) * sizeof(TCHAR);
						HGLOBAL hgbl	= GlobalAlloc(GMEM_MOVEABLE, nSize);
						LPTSTR szData	= (LPTSTR)GlobalLock(hgbl);
						_tcscpy(szData, szTmp);
						GlobalUnlock(hgbl);
						SetClipboardData(CF_TEXT, hgbl);
						
						CloseClipboard();
					}
					break;
				}
				case IDC_BTNGETDATA:
				{
					if (OpenClipboard(hWnd))
					{
						TCHAR szBuf[MAX_PATH] = {0};
						HGLOBAL hGlobal = GetClipboardData(CF_TEXT);
						LPTSTR  szData	= (LPTSTR)GlobalLock(hGlobal);
						MessageBox(hWnd, szData, _T("ClipBoard"), MB_OK);
						GlobalUnlock(hGlobal);
						CloseClipboard();
					}

					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}
	return DefWindowProc(hWnd, nMessage, wParam, lParam);
}

HWND CreateWnd(const TCHAR* szClsName, const TCHAR* szWndName)
{
	const UINT nWidth	= 800;
	const UINT nHeight	= 480;
	const UINT nLeft	= ((GetSystemMetrics(SM_CXSCREEN) - nWidth) >> 1);
	const UINT nTop		= ((GetSystemMetrics(SM_CYSCREEN) - nHeight) >> 1);
	return CreateWindow(szClsName, szWndName, WS_VISIBLE|WS_OVERLAPPEDWINDOW, nLeft, nTop, nWidth, nHeight, NULL, NULL, GetModuleHandle(NULL), NULL);
}

void RunMessageLoop()
{
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}


bool RegisterWndClass(const TCHAR* szClsName)
{
	WNDCLASS wc			= {0};
	wc.style			= CS_VREDRAW|CS_HREDRAW;
	wc.lpszClassName	= szClsName;
	wc.lpfnWndProc		= WndProc;
	wc.hInstance		= GetModuleHandle(NULL);
	wc.hCursor			= (HCURSOR)LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wc.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);

	return (0 != RegisterClass(&wc));
}



BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName	= _T("EveryDayDemo");
	const TCHAR* szWndName	= _T("Demo0056");
	if (RegisterWndClass(szClsName))
	{
		HWND hWnd = CreateWnd(szClsName, szWndName);
		if (NULL != hWnd)
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
		}
		RunMessageLoop();
	}

	return TRUE;
}